<?php
namespace Home\Controller;
use Think\Controller;
class NavController extends ExtendController{
	public function tree(){
        $this->_cmd('_tree_');
    }
    
    protected  function _tree_list(){
    	$nav=M('nav');
    	$res=$nav->select();
    	$tmp=array();
    	foreach ($res as $k=>$v){
    		$tmp[$v['pid']][]=$v;
    	}
    	$treeData=$this->createTreeData($tmp,0);
    	$this->ajaxRespond($treeData);
    	//树的数据类型[{"nav_id":"1","value":"","title":"\u7528\u6237\u7ba1\u7406","pid":"0","node_type":"2","id":"","text":"\u7528\u6237\u7ba1\u7406","children":[{"nav_id":"2","value":"userList","title":"\u7528\u6237\u5217\u8868","pid":"1","node_type":"1","id":"Sys-userList","text":"\u7528\u6237\u5217\u8868","leaf":true}]},{"nav_id":"3","value":"","title":"\u5e16\u5b50\u7ba1\u7406","pid":"0","node_type":"2","id":"","text":"\u5e16\u5b50\u7ba1\u7406","children":[{"nav_id":"4","value":"msgList","title":"\u7559\u8a00\u5217\u8868","pid":"3","node_type":"1","id":"Sys-msgList","text":"\u7559\u8a00\u5217\u8868","leaf":true}]}]
    }
    
    public function createTreeData($data,$pid=0){
    	if($data[$pid]){
    		foreach ($data[$pid] as $k=>$v){
    			$id=$v['nav_id'];
    			$v["text"]=$v["title"];
    			if($data[$id]){
    				$v["children"]=$this->createTreeData($data,$id);
    			}else{
    				$v["leaf"]=true;
    			}
    			$arr[]=$v;
    		}
    		return $arr;
    	}
    }  
    
    //添加节点
    protected  function _tree_addNode(){
    	$data=I('post.',array());
    	$nav=D('nav');
    	$tmp=array(
    		'title'=>$data['title'],
    	    'pid'=>$data['selectId']
    	);
    	$result=$nav->where(array(
    		 'title'=>$data['title']
    	))->find();
    	if(empty($result['title'])){
    		$result=$nav->add($tmp);
    		$this->ajaxRespond(array('success'=>true));
    	}
    	$this->ajaxRespond(array('success'=>false));
    }
   
}